package com.yiyw.herostory.handler;

import com.yiyw.herostory.bo.UserBo;
import com.yiyw.herostory.manage.UserManage;
import com.yiyw.herostory.proto.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * @Author yiyw
 * @Description 还有谁在场事件
 * @Date 2022/8/14 14:50
 **/
public class WhoElseIsHereCmdHandler implements ICmdHandler<GameMsgProtocol.WhoElseIsHereCmd> {

    @Override
    public void handler(ChannelHandlerContext ctx, GameMsgProtocol.WhoElseIsHereCmd msg) {
        // 查询还有哪个用户在场
        GameMsgProtocol.WhoElseIsHereResult.Builder result = GameMsgProtocol.WhoElseIsHereResult.newBuilder();
        // 获取所有的用户
        List<UserBo> allUser = UserManage.getAllUser();
        for (UserBo userBo : allUser) {
            GameMsgProtocol.WhoElseIsHereResult.UserInfo.Builder userInfo =
                    GameMsgProtocol.WhoElseIsHereResult.UserInfo.newBuilder();
            userInfo.setUserId(userBo.getUserId());
            userInfo.setHeroAvatar(userBo.getHeroAvatar());
            GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState.Builder moveState =
                    GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState.newBuilder();
            moveState.setFromPosX(userBo.getMoveFromPosX());
            moveState.setFromPosY(userBo.getMoveFromPosY());
            moveState.setToPosX(userBo.getMoveToPosX());
            moveState.setToPosY(userBo.getMoveToPosY());
            moveState.setStartTime(userBo.getMoveStartTime());
            userInfo.setMoveState(moveState);
            result.addUserInfo(userInfo.build());
        }
        GameMsgProtocol.WhoElseIsHereResult build = result.build();
        // 返回结果
        ctx.writeAndFlush(build);
    }
}
